package jsyf.oa.web.email;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.email.EmailFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by qiaox on 2017/10/25.
 */
@SuppressWarnings("all")
@Api(value = "邮件接口", description = "发送邮件功能")
@Controller
@RequestMapping("/email")
public class EmailController {

    @Resource
    private EmailFacade emailFacade;


    /**
     * @description: 发送邮件
     * @author: @崔程鵬 @cuichengpeng
     * @creat: 10:07  2017/11/1 0001
     * @since: V1.0.0
     * @see jsyf.oa.web
     */
    @ResponseBody
    @RequestMapping("/sendEmail.json")
    @ApiOperation(
            value = "发送邮件",
            notes = "发送邮件功能",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonData", value = "{'recievers':[{'id':'用戶id','name':'姓名'}]," +
                    "'copiers':[{'id':'用户id','name':'姓名'},]," +
                    "'attaches':[{'id':'附件id','name':'附件.txt','url':'地址'}]," +
                    "'subject':'主题','content':'内容'}", dataType = "String", paramType = "body")
    })
    public void sendEmail(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.emailFacade.sendEmail(jsonData);
        JSONObject jsonResult = new JSONObject();
        jsonResult.put("id", resultId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonResult));
    }

    /**
     * 获取公司-部门-职位树形结构
     */
    @ApiOperation(
            value = "获取公司-部门-职位-人员树形结构",
            notes = "获取公司-部门-职位-人员树形结构",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonData", value = "", dataType = "参数类型（string）", paramType = "参数类型（json）")
    })
    @ResponseBody
    @RequestMapping(value = "/getOrgInbox.json", method = RequestMethod.POST)
    public void getOrgInbox(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj_params = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = obj_params.getString("userId");
        List<JSONObject> records = this.emailFacade.getOrgInbox(userId);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    /**
     * @description: 收件箱列表
     * @author: @崔程鵬 @cuichengpeng
     * @creat: 14:08  2017/11/10 0010
     * @since: V1.0.0
     * @see jsyf.oa.web
     */
    @ApiOperation(
            value = "收件箱列表查询",
            notes = "收件箱列表查询",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{requestPage: 1, pageSize: 30, searchKey: \"\", userId: \"2017112017064B52B5C5842EBD1D598D\"}", dataType = "String", paramType = "body")
    @ResponseBody
    @RequestMapping(value = "/findInboxList.json", method = RequestMethod.POST)
    public void getList(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj_params = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        JSONObject records;
        String requestPage;
        String pageSize;
        if (obj_params.containsKey("requestPage") && obj_params.containsKey("pageSize")) {
            requestPage = obj_params.getString("requestPage");
            pageSize = obj_params.getString("pageSize");
            records = this.emailFacade.findInboxList(obj_params, Integer.parseInt(requestPage), Integer.parseInt(pageSize));
        } else {
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
            records = this.emailFacade.findInboxList(obj_params, Integer.parseInt(requestPage), Integer.parseInt(pageSize));
        }
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }

    }

    /**
     * @description: 收件箱详情
     * @author: @崔程鵬 @cuichengpeng
     * @creat: 16:11  2017/11/8 0008
     * @since: V1.0.0
     * @see jsyf.oa.web
     */
    @ApiOperation(
            value = "收件箱详情查询",
            notes = "收件箱详情查询",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{emailId: \"20171124105740D2B6EF034072DDEC62\"}", dataType = "String", paramType = "body")
    @ResponseBody
    @RequestMapping(value = "/findInboxDetail.json", method = RequestMethod.POST)
    public void findInboxDetail(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String emailId = jsonParams.getString("emailId");
        JSONObject records = this.emailFacade.findInboxDetail(emailId, userId);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    /**
     * @description: 未读邮件数量
     * @author: @崔程鵬 @cuichengpeng
     * @creat: 21:20  2017/12/20 0020
     * @since: V1.0.0
     * @see jsyf.oa.web
     */
    @ApiOperation(
            value = "未读邮件数量",
            notes = "未读邮件数量",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    @ResponseBody
    @RequestMapping(value = "/unreadEmail.json", method = RequestMethod.POST)
    public void unreadEmail(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        Integer records = this.emailFacade.unreadEmail(userId);
        JSONObject result = new JSONObject();
        result.put("unread", records);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/setReadCloseTime.json", method = RequestMethod.POST)
    public void setReadCloseTime(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = emailFacade.setReadCloseTime(jsonParam.getString("userId"), jsonParam.getString("emailId"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
    }

    @RequestMapping(value = "/findEmailAll.json", method = RequestMethod.POST)
    public void findEmailAll(HttpServletResponse response, @RequestBody String jsonData) {
        boolean result = emailFacade.findEmailAll(jsonData);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }

    @RequestMapping(value = "/getNotReadReport.json", method = RequestMethod.POST)
    public void getNotReadReport(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = emailFacade.getNotReadReport();
        if (null != result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
        }else{
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("错误"));
        }
    }
}
